package org.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.po.ErpOrganization;
import org.example.domain.vo.ErpOrganizationVO;
import org.example.service.ErpOrganizationService;
import org.example.mapper.ErpOrganizationMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author DELL
* @description 针对表【erp_organization(机构表)】的数据库操作Service实现
* @createDate 2024-11-05 20:21:48
*/
@Service
public class ErpOrganizationServiceImpl extends ServiceImpl<ErpOrganizationMapper, ErpOrganization>
    implements ErpOrganizationService{
    
    @Override
    public List<ErpOrganizationVO> selectOrganVOTree() {
        return baseMapper.selectOrganVOTree(0L);
    }
    
    @Override
    public List<ErpOrganizationVO> queryParent(Long id, Long parentId, Long tenantId) {
        return baseMapper.queryParent(id, parentId, tenantId);
    }
    
    @Override
    public boolean updateOrgan(ErpOrganization organ) {
        boolean update = lambdaUpdate().eq(ErpOrganization::getId, organ.getId())
                .set(ErpOrganization::getOrgNo, organ.getOrgNo())
                .set(ErpOrganization::getOrgAbr, organ.getOrgAbr())
                .set(ErpOrganization::getParentId, organ.getParentId())
                .set(ErpOrganization::getSort, organ.getSort())
                .set(ErpOrganization::getRemark, organ.getRemark())
                .set(ErpOrganization::getUpdateTime, new Date())
                .update();
        return update;
    }
    
    @Override
    public boolean organInsert(ErpOrganization organization) {
        if (BeanUtil.isEmpty(organization.getParentId())) organization.setParentId(0L);
        organization.setCreateTime(new Date());
        organization.setUpdateTime(new Date());
        return save(organization);
    }
    
    @Override
    public boolean removeBatchByOrgAbs(List<String> organAbs, Long tenantId) {
        LambdaUpdateWrapper<ErpOrganization> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ErpOrganization::getOrgAbr, organAbs)
                .eq(ErpOrganization::getTenantId, tenantId);
        return remove(wrapper);
    }
}




